package com.hunan.api.catv.factory;

import com.hunan.api.catv.entity.enums.LoginType;
import com.hunan.api.catv.service.biz.member.LoginService;
import com.hunan.api.catv.service.biz.member.login.AppleLoginService;
import com.hunan.api.catv.service.biz.member.login.OneKeyLoginService;
import com.hunan.api.catv.service.biz.member.login.SmsLoginService;
import com.hunan.api.catv.service.biz.member.login.WechatLoginService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class LoginFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;


    private Map<Integer, LoginService> serviceMap = new HashMap<Integer, LoginService>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public LoginService getLoginService(Integer loginTyoe) {
        return serviceMap.get(loginTyoe);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        Map<String, LoginService> beanMap = applicationContext.getBeansOfType(LoginService.class);

        beanMap.forEach((k, v) -> {
            if (v instanceof SmsLoginService) {
                serviceMap.put(LoginType.sms.getCode(), v);
            }
            if (v instanceof WechatLoginService) {
                serviceMap.put(LoginType.wechat.getCode(), v);
            }
            if (v instanceof AppleLoginService) {
                serviceMap.put(LoginType.apple.getCode(), v);
            }
            if (v instanceof OneKeyLoginService) {
                serviceMap.put(LoginType.jiguang.getCode(), v);
            }
        });
    }
}